﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;

namespace .BizLogic
{
    using RDIFramework.BizLogic;
    using RDIFramework.Utilities;

    /// <summary>
    /// pmwebsitecolumnManager
    /// pm-站点栏目
    /// 
    /// 修改纪录
    /// 
    /// 2025-01-18 版本：3.5  创建主键。
    /// 
    /// 版本：3.5
    /// 
    /// <author>
    /// <name></name>
    /// <date>2025-01-18</date>
    /// </author>
    /// </summary>
    public partial class pmwebsitecolumnManager : DbCommonManager
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public pmwebsitecolumnManager()
        {
            base.CurrentTableName = pmwebsitecolumnTable.TableName;
            base.PrimaryKey = "Id";
        }

        /// <summary>
        /// 构造函数
        /// <param name="tableName">指定表名</param>
        /// </summary>
        public pmwebsitecolumnManager(string tableName)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        public pmwebsitecolumnManager(IDbProvider dbProvider): this()
        {
            DBProvider = dbProvider;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        public pmwebsitecolumnManager(UserInfo userInfo) : this()
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        public pmwebsitecolumnManager(IDbProvider dbProvider, UserInfo userInfo) : this(dbProvider)
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        /// <param name="tableName">指定表名</param>
        public pmwebsitecolumnManager(IDbProvider dbProvider, UserInfo userInfo, string tableName) : this(dbProvider, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        /// <returns>主键</returns>
        public string Add(pmwebsitecolumnEntity pmwebsitecolumnEntity)
        {
            return this.AddEntity(pmwebsitecolumnEntity);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        /// <param name="identity">自增量方式</param>
        /// <param name="returnId">返回主键</param>
        /// <returns>主键</returns>
        public string Add(pmwebsitecolumnEntity pmwebsitecolumnEntity, bool identity, bool returnId)
        {
            this.Identity = identity;
            this.ReturnId = returnId;
            return this.AddEntity(pmwebsitecolumnEntity);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        public int Update(pmwebsitecolumnEntity pmwebsitecolumnEntity)
        {
            return this.UpdateEntity(pmwebsitecolumnEntity);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键</param>
        public pmwebsitecolumnEntity GetEntity(string id)
        {
            return GetEntity(int.Parse(id));
        }

        public pmwebsitecolumnEntity GetEntity(int id)
        {
            return BaseEntity.Create<pmwebsitecolumnEntity>(this.GetDT(pmwebsitecolumnTable.FieldId, id));
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        public string AddEntity(pmwebsitecolumnEntity pmwebsitecolumnEntity)
        {
            string sequence = string.Empty;
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider, this.Identity, this.ReturnId);
            sqlBuilder.BeginInsert(this.CurrentTableName, pmwebsitecolumnTable.FieldId);
            if (!this.Identity) 
            {
                sqlBuilder.SetValue(pmwebsitecolumnTable.FieldId, pmwebsitecolumnEntity.Id);
            }
            else
            {
                if (!this.ReturnId && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                {
                    if (DBProvider.CurrentDbType == CurrentDbType.Oracle)
                    {
                        sqlBuilder.SetFormula(pmwebsitecolumnTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
                    }
                    if (DBProvider.CurrentDbType == CurrentDbType.DB2)
                    {
                        sqlBuilder.SetFormula(pmwebsitecolumnTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
                    }
                }
                else
                {
                    if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                    {
                        if (pmwebsitecolumnEntity.Id == null)
                        {
                            if (string.IsNullOrEmpty(sequence))
                            {
                                CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                                sequence = sequenceManager.GetSequence(this.CurrentTableName);
                            }
                            pmwebsitecolumnEntity.Id = int.Parse(sequence);
                        }
                        sqlBuilder.SetValue(pmwebsitecolumnTable.FieldId, pmwebsitecolumnEntity.Id);
                    }
                }
            }
            this.SetEntity(sqlBuilder, pmwebsitecolumnEntity);
            if (UserInfo != null) 
            { 
                sqlBuilder.SetValue(pmwebsitecolumnTable.FieldCreateBy, UserInfo.RealName);
            } 
            if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.SqlServer || DBProvider.CurrentDbType == CurrentDbType.Access))
            {
                sequence = sqlBuilder.EndInsert().ToString();
            }
            else
            {
                sqlBuilder.EndInsert();
            }
            return sequence;
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        public int UpdateEntity(pmwebsitecolumnEntity pmwebsitecolumnEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
            sqlBuilder.BeginUpdate(this.CurrentTableName);
            this.SetEntity(sqlBuilder, pmwebsitecolumnEntity);
            sqlBuilder.SetWhere(pmwebsitecolumnTable.FieldId, pmwebsitecolumnEntity.Id);
            return sqlBuilder.EndUpdate();
        }

        /// <summary>
        /// 设置实体
        /// </summary>
        /// <param name="sqlBuilder">sql语句生成器</param>
        /// <param name="pmwebsitecolumnEntity">实体</param>
        private void SetEntity(SQLBuilder sqlBuilder, pmwebsitecolumnEntity pmwebsitecolumnEntity)
        {
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldColumnName, pmwebsitecolumnEntity.ColumnName);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldParentId, pmwebsitecolumnEntity.ParentId);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldAncestors, pmwebsitecolumnEntity.Ancestors);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldWebsiteId, pmwebsitecolumnEntity.WebsiteId);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldBreadcrumb, pmwebsitecolumnEntity.Breadcrumb);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldDisplayPosition, pmwebsitecolumnEntity.DisplayPosition);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldCreateTime, pmwebsitecolumnEntity.CreateTime);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldUpdateTime, pmwebsitecolumnEntity.UpdateTime);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldUpdateBy, pmwebsitecolumnEntity.UpdateBy);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldSortnum, pmwebsitecolumnEntity.Sortnum);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldPinyin, pmwebsitecolumnEntity.Pinyin);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldStatus, pmwebsitecolumnEntity.Status);
            sqlBuilder.SetValue(pmwebsitecolumnTable.FieldRemark, pmwebsitecolumnEntity.Remark);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(int id)
        {
            return this.Delete(new KeyValuePair<string, object>(pmwebsitecolumnTable.FieldId, id));
        }
    }
}
